package org.jeecg.modules.oa.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.SsoUtil;
import org.jeecg.common.util.StringUtils;
import org.jeecg.modules.oa.entity.OaReportPlan;
import org.jeecg.modules.oa.entity.OaReportWeek;
import org.jeecg.modules.oa.mapper.OaReportWeekMapper;
import org.jeecg.modules.oa.service.IOaReportPlanService;
import org.jeecg.modules.oa.service.IOaReportWeekService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description: 周报表
 * @Author: jeecg-boot
 * @Date:   2019-09-17
 * @Version: V1.0
 */
@Service
public class OaReportWeekServiceImpl extends ServiceImpl<OaReportWeekMapper, OaReportWeek> implements IOaReportWeekService {
    @Autowired
    private OaReportWeekMapper reportWeekMapper;

    @Autowired
    private IOaReportPlanService reportPlanService;

    @Value("${web.imageFileUrl}")
    private String imageFileUrl;

    @Override
    public Page<OaReportWeek> find(Page<OaReportWeek> page, OaReportWeek oaReportWeek,String departIds) {
        List<OaReportWeek> list = reportWeekMapper.findAllList(page,oaReportWeek,departIds);
        LoginUser currentUser = SsoUtil.getLoginUser();
        for (OaReportWeek reportWeek : list){
            processData(reportWeek,currentUser.getId());
        }
        return page.setRecords(list);
    }

    @Override
    public Page<OaReportWeek> findSelfList(Page<OaReportWeek> page, OaReportWeek oaReportWeek) {
        List<OaReportWeek> list = reportWeekMapper.findSelfList(page,oaReportWeek);
        LoginUser currentUser = SsoUtil.getLoginUser();
        for (OaReportWeek reportWeek : list){
            processData(reportWeek,currentUser.getId());
        }
        return page.setRecords(list);
    }

    @Override
    public OaReportWeek processData(OaReportWeek reportWeek,String userId){
        Date now = new Date();
        boolean isNewest = false;
        if (DateUtils.isEffectiveDate(now, reportWeek.getWorkDateFrom(), reportWeek.getWorkDateTo()) && userId.equals(reportWeek.getCreateBy()))
            isNewest = true;
        reportWeek.setIsNewest(isNewest);
        reportWeek.setCurrentUserId(userId);

        List<OaReportPlan> thisWeekList = Lists.newArrayList();// 本周完成事项
        List<OaReportPlan> nextWeekList = Lists.newArrayList();// 下周计划事项
        List<OaReportPlan> sList = reportPlanService.getThisWeekPlan(reportWeek.getCreateBy(),reportWeek.getWorkDateTo()); // 获取当前周报的创建者上周的计划
        if (sList.size() > 0) {
            thisWeekList.addAll(sList);
        }
        if (StringUtils.isNotBlank(reportWeek.getId())) {
            List<OaReportPlan> reportWeekPlanList = reportPlanService.getReportWeekPlanById(reportWeek.getId()); // 获取到这周的任务和下周计划
            if (reportWeekPlanList.size() > 0) {
                for (int i = 0; i < reportWeekPlanList.size(); i++) {
                    if (reportWeekPlanList.get(i).getPlanStatus().equals("0")) {// 本周的计划
                        thisWeekList.add(reportWeekPlanList.get(i));
                    } else {
                        nextWeekList.add(reportWeekPlanList.get(i));
                    }
                }
            }
            reportWeek.setThisWeekList(thisWeekList);
            reportWeek.setNextWeekList(nextWeekList);
        }
        return reportWeek;
    }
    @Override
    public List<OaReportWeek> findAllListToOutSource(OaReportWeek reportWeek) {
        return reportWeekMapper.findAllListToOutSource(reportWeek);
    }

    @Override
    public OaReportWeek getReportWeekById(String id) {
        OaReportWeek results = reportWeekMapper.selectById(id);
        if (results == null) {
            return results;
        }
        //checkReportWeekVersion(results);  //公共部门代码抽取 -- 校验周报版本的新旧（目的为了兼容旧版本）
        return results;
    }

    @Override
    public OaReportWeek findByCreateBYAndNew(String id, Date date) {
        return reportWeekMapper.findByCreateBYAndNew(id,date);
    }

    @Override
    @Transactional
    public void editReportWeek(OaReportWeek reportWeek) {
        updateById(reportWeek);
        Date workDateFromNext = DateUtils.getDateStart(DateUtils.addDate(reportWeek.getWorkDateFrom(), 7));
        Date workDateToNext = DateUtils.getDateEnd(DateUtils.addDate(reportWeek.getWorkDateTo(), 7));
        reportPlanService.savePlanWeek(reportWeek.getId(), workDateFromNext, workDateToNext, reportWeek.getNextWeekList(),"1");
        reportPlanService.savePlanWeek(reportWeek.getId(), reportWeek.getWorkDateFrom(), reportWeek.getWorkDateTo(), reportWeek.getThisWeekList(),"0");
    }

    @Override
    @Transactional
    public void saveReportWeek(OaReportWeek reportWeek) {
        save(reportWeek);
        reportPlanService.savePlanWeek(reportWeek.getId(), reportWeek.getWorkDateFrom(), reportWeek.getWorkDateTo(), reportWeek.getThisWeekList(),"0");
        Date workDateFrom1 = DateUtils.getDateStart(DateUtils.addDate(reportWeek.getWorkDateFrom(), 7));
        Date workDateTo1 = DateUtils.getDateEnd(DateUtils.addDate(reportWeek.getWorkDateTo(), 7));
        reportPlanService.savePlanWeek(reportWeek.getId(), workDateFrom1, workDateTo1, reportWeek.getNextWeekList(),"1");

    }


    /**
     * 获取周报信息
     *
     * @param reportWeek
     * @return
     */
    @Override
    public OaReportWeek findByUserNo(OaReportWeek reportWeek) {
        OaReportWeek results = reportWeekMapper.findByUserNo(reportWeek);
        return results;
    }

}
